﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Mappers
{
    public class CmsContentDataMapper : BaseSqlEntityDataMapper<CmsContent, int>
    {
        private static BaseSqlEntityDataMapper<CmsContent, int> _based = new BaseSqlEntityDataMapper<CmsContent, int>(new string[] { "ContentType", "SystemLanguage", "SiteGroup", "Site", "SiteCategory", "EntityMetaInformation" });

        private static Dictionary<string, Func<object, IList<SqlParameter>>> _mappersTo = new Dictionary<string, Func<object, IList<SqlParameter>>>();

        static CmsContentDataMapper()
        {

        }

        public CmsContentDataMapper()
            : base()
        { }

        public override IList<SqlParameter> MapTo(string sp, object parameter, bool isForCreate)
        {
            Guard.ArgumentNotEmpty(sp, "sp");
            Guard.ArgumentNotNull(parameter, "parameter");



            var parameters = _mappersTo.ContainsKey(sp) ?
                _mappersTo[sp](parameter) :
                _based.MapTo(sp, parameter, isForCreate);

            if (sp == "cms_sp_Content_Save")
            {
                var idParam = parameters.FirstOrDefault(p => p.ParameterName == "@Id");
                if (idParam != null)
                {
                    idParam.Direction = ParameterDirection.InputOutput;
                }
            }

            return parameters;
        }
    }
}
